/**
 * 
 */
package com.lanswon.service;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanswon.common.enums.MessageType;
import com.lanswon.common.enums.StaticMessage;
import com.lanswon.common.enums.UserType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.common.util.TokenProccessor;
import com.lanswon.dao.UserAuthsMapper;
import com.lanswon.dao.UserMapper;
import com.lanswon.entity.UserVO;

/**
 * @author lanswon
 *
 */
@Service
public class AdminService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserAuthsMapper userAuthsMapper;
	

	/**
	 * @param identifier
	 * @param credential
	 * @param request
	 * @return
	 */
	public Map<String, Object> login(String identifier, String credential, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 参数校验
		if (StringUtil.isEmpty(identifier) || StringUtil.isEmpty(credential)) {
			resultMap.put("code", 1);
			resultMap.put("message", MessageType.MT011);
			return resultMap;
		}
		try {
			// 验证密码
			credential = StringUtil.md5(credential);
			String accountType = accountType(identifier, request);
			Long userId = userAuthsMapper.getUserAuths(accountType, identifier, credential);
			if (userId == null) {
				resultMap.put("code", 1);
				resultMap.put("message", MessageType.MT010);
				return resultMap;
			}

			// 验证状态
			UserVO userVO = userMapper.selectByPrimaryKey(userId);
			Map map = new HashMap();
			if ("1".equals(userVO.getUserStatus())) {
				StringUtil.putIntoMap(map, "userId", userId);
				StringUtil.putIntoMap(map, "userName", userVO.getUserName());
				StringUtil.putIntoMap(map, "email", userVO.getEmail());
				StringUtil.putIntoMap(map, "phone", userVO.getMobile());
				resultMap.put("code", 0);
				resultMap.put("message", MessageType.MT007);
				resultMap.put("data", map);
				
				request.getSession().setAttribute("userId", userId);
				request.getSession().setAttribute("userName", userVO.getUserName());
			} else if ("2".equals(userVO.getUserStatus())) {
				resultMap.put("code", 1);
				resultMap.put("message", MessageType.MT009);
			}
			String token = TokenProccessor.getInstance().makeToken();
			token = token + System.currentTimeMillis();
			userVO.setToken(token);
			userMapper.updateByPrimaryKeySelective(userVO);
			request.getSession().setAttribute("apptoken", token);
		} catch (Exception e) {
			resultMap.put("code", 0);
			resultMap.put("message", MessageType.MT008);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * @param identifier
	 * @return
	 */
	private String accountType(String identifier, HttpServletRequest request) {
		if (request.getRequestURL().indexOf("admin") != -1) {
			return UserType.UT001.getValue();
		} else {
			return UserType.UT002.getValue();
		}
	}

	/**
	 * @param request
	 * @return
	 */
	public Map<String, Object> withdraw(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		request.getSession().invalidate();
		resultMap.put("code", 0);
		return resultMap;
	}

}
